package com.zretc.internalreferral.service;

import com.mysql.cj.MessageBuilder;
import com.zretc.api.pojo.InternalReferral;
import com.zretc.api.pojo.Letter;
import com.zretc.common.constant.LetterTemplatesConstant;
import com.zretc.internalreferral.dao.InternalReferralMapper;
import com.zretc.letter.service.LetterService;
import com.zretc.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@Transactional
@Slf4j
public class InternalReferralService {
    @Autowired
    private InternalReferralMapper internalReferralMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    public InternalReferral queryIRById(Integer id)
    {
        String redisKey="ir:"+id;
        InternalReferral result=redisService.getCacheObject(redisKey);
        if (Objects.isNull(result))
        {
            result=internalReferralMapper.getIRById(id);
            if (Objects.isNull(result))return null;
            redisService.setCacheObject(redisKey,result);
        }
        redisService.expire(redisKey,5);
        return result;
    }
    public List<InternalReferral> queryIRsByAmount(Integer index,Integer pageNum)
    {
        String redisKey="irList:"+index+","+pageNum;
        List<InternalReferral> result=redisService.getCacheList(redisKey);
        if (CollectionUtils.isEmpty(result))
        {
            result=internalReferralMapper.getIRsByAmount(index, pageNum);
            if (CollectionUtils.isEmpty(result))return null;
            redisService.setCacheList(redisKey,result);
        }
        redisService.expire(redisKey,5);
        return result;
    }
    public List<InternalReferral> queryIRsByAmountByCreatorId(Integer index,Integer pageNum,Integer createdBy)
    {
        List<InternalReferral> list=internalReferralMapper.getIRsByAmountByCreatorId(index, pageNum, createdBy);
        return list;
    }
    public boolean updateIROfUser(InternalReferral internalReferral,Integer createdBy)
    {
        return internalReferralMapper.updateSingleRecordOfUser(internalReferral,createdBy)==1;
    }
    public boolean publishIR(InternalReferral internalReferral)
    {
        return internalReferralMapper.addSingleRecord(internalReferral)==1;
    }
    public boolean deleteIROfUser(Integer id,Integer userId)
    {
        return internalReferralMapper.deleteSingleRecordOfUser(id,userId)==1;
    }
    public boolean topIR(Integer id,boolean isTopped)
    {
        return isTopped ? internalReferralMapper.updateIsToppedToTrueById(id) == 1
                : internalReferralMapper.updateIsToppedToFalseById(id) == 1;
    }
    public boolean approvalIR(Integer id,Integer passed,Integer toUserId,String content)
    {
        if(internalReferralMapper.updateIsPassedById(id, passed)==1)
        {
            Date date=new Date();
            Letter letter=new Letter(null, content,LetterTemplatesConstant.APPROVAL_NOTIFY_TITLE
                    ,0,date,false);
            Map<String,Object> map=new HashMap<>();
            map.put("letter",letter);
            map.put("toUserId",toUserId);
            rocketMQTemplate.asyncSend("letter:approvalMsg", map, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("发送消息成功");
                }

                @Override
                public void onException(Throwable throwable) {
                    log.error("发送消息失败");
                }
            });
            return true;
        }
        return false;
    }
}
